import 'package:flutter/material.dart';
import 'package:netflix_social/theme/app_theme.dart';
import 'package:video_player/video_player.dart';

class CustomVideoControls extends StatelessWidget {
  final VideoPlayerController controller;
  final bool isPlaying;
  final Duration currentPosition;
  final Duration videoDuration;
  final VoidCallback onPlayPause;
  final Function(double) onSliderChanged;
  final Function(double) onSliderChangeEnd;
  final VoidCallback onForward;
  final VoidCallback onRewind;
  final VoidCallback onFullScreenToggle;
  final bool isFullScreen;

  const CustomVideoControls({
    Key? key,
    required this.controller,
    required this.isPlaying,
    required this.currentPosition,
    required this.videoDuration,
    required this.onPlayPause,
    required this.onSliderChanged,
    required this.onSliderChangeEnd,
    required this.onForward,
    required this.onRewind,
    required this.onFullScreenToggle,
    required this.isFullScreen,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        // 顶部控制栏
        _buildTopControls(context),
        
        // 中间控制
        _buildCenterControls(),
        
        // 底部控制栏
        _buildBottomControls(context),
      ],
    );
  }

  Widget _buildTopControls(BuildContext context) {
    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Row(
          children: [
            IconButton(
              icon: const Icon(
                Icons.arrow_back,
                color: Colors.white,
                size: 24,
              ),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            const Spacer(),
            IconButton(
              icon: const Icon(
                Icons.settings,
                color: Colors.white,
                size: 24,
              ),
              onPressed: () {
                // 打开设置
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCenterControls() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        IconButton(
          icon: const Icon(
            Icons.replay_10,
            color: Colors.white,
            size: 40,
          ),
          onPressed: onRewind,
        ),
        const SizedBox(width: 24),
        IconButton(
          icon: Icon(
            isPlaying ? Icons.pause_circle : Icons.play_circle,
            color: Colors.white,
            size: 60,
          ),
          onPressed: onPlayPause,
        ),
        const SizedBox(width: 24),
        IconButton(
          icon: const Icon(
            Icons.forward_10,
            color: Colors.white,
            size: 40,
          ),
          onPressed: onForward,
        ),
      ],
    );
  }

  Widget _buildBottomControls(BuildContext context) {
    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            // 进度信息
            Row(
              children: [
                Text(
                  _formatDuration(currentPosition),
                  style: const TextStyle(color: Colors.white),
                ),
                Expanded(
                  child: Slider(
                    value: currentPosition.inMilliseconds.toDouble(),
                    min: 0.0,
                    max: videoDuration.inMilliseconds.toDouble(),
                    activeColor: AppTheme.netflixRed,
                    inactiveColor: Colors.grey[700],
                    onChanged: onSliderChanged,
                    onChangeEnd: onSliderChangeEnd,
                  ),
                ),
                Text(
                  _formatDuration(videoDuration),
                  style: const TextStyle(color: Colors.white),
                ),
              ],
            ),
            
            // 底部按钮行
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // 左侧信息
                const Text(
                  'HD',
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                
                // 右侧按钮
                Row(
                  children: [
                    IconButton(
                      icon: const Icon(
                        Icons.subtitles,
                        color: Colors.white,
                      ),
                      onPressed: () {
                        // 字幕设置
                      },
                    ),
                    IconButton(
                      icon: Icon(
                        isFullScreen 
                            ? Icons.fullscreen_exit 
                            : Icons.fullscreen,
                        color: Colors.white,
                      ),
                      onPressed: onFullScreenToggle,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  String _formatDuration(Duration duration) {
    String twoDigits(int n) => n.toString().padLeft(2, '0');
    String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
    String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
    return '${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds';
  }
}
